const Mock = require('mockjs');
const tokens = {
  admin: {
    token: "admin-token"
  },
  editor: {
    token: "editor-token"
  }
};

const users = {
  "admin-token": {
    roles: ["admin"],
    introduction: "I am a super administrator",
    avatar: "https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif",
    name: "Super Admin"
  },
  "editor-token": {
    roles: ["editor"],
    introduction: "I am an editor",
    avatar: "https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif",
    name: "Normal Editor"
  }
};

export default [
  // user login
  {
    path: "/auth/signIn",
    method: "post",
    handle: (req, res) => {
      const { identifier } = req.body;
      const token = tokens[identifier];

      // mock error
      if (!token) {
        return res.send({
          code: 401,
          message: "Account and password are incorrect."
        });
      }

      res.send({
        code: 0,
        message: "ok",
        data: token
      });
    }
  },

  // get user info
  {
    path: "/my/info",
    method: "get",
    handle: (req, res) => {
      const { authorization: token } = req.headers;
      const info = users[token];

      // mock error
      if (!info) {
        return res.send({
          code: 50008,
          message: "Login failed, unable to get user details."
        });
      }
      res.send({
        code: 0,
        message: "ok",
        data: info
      });
    }
  },

  // get users
  {
    path: "/users",
    method: "get",
    handle: (req, res) => {
      res.send({
        code: 0,
        message: "ok",
        data: {
          total: 20,
          'items|20': [{
            order_no: '@guid()',
            timestamp: +Mock.Random.date('T'),
            username: '@name()',
            price: '@float(1000, 15000, 0, 2)',
            'status|1': ['success', 'pending']
          }]
        }
      });
    }
  },

  // get user id
  {
    path: "/user/:id",
    method: "get",
    handle: (req, res) => {
      const id = req.params.id;
      res.send({
        code: 0,
        message: "ok",
        data: { id }
      });
    }
  },

  // user logout
  {
    path: "/auth/logout",
    method: "post",
    handle: (req, res) => {
      const { identifier } = req.body;
      const token = tokens[identifier];

      // mock error
      if (!token) {
        return res.send({
          code: 60204,
          message: "Account and password are incorrect."
        });
      }
      res.send({
        code: 20000,
        data: token
      });
    }
  },

  // user status
  {
    path: "/auth/status",
    method: "get",
    handle: (req, res) => {
      res.end('ok');
    }
  }
];
